package com.qf.oa_system.controller;

import com.qf.oa_system.entity.Leaves;
import com.qf.oa_system.entity.Teacher;
import com.qf.oa_system.entity.User;
import com.qf.oa_system.service.LeavesService;
import com.qf.oa_system.service.TeacherService;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * ???ٱ(Leaves)表控制层
 *
 * @author makejava
 * @since 2020-06-03 20:26:23
 */
@Controller
@RequestMapping("leaves")
public class LeavesController {
    /**
     * 服务对象
     */
    @Resource
    private LeavesService leavesService;

    @Resource
    private TeacherService teacherService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Leaves selectOne(Integer id) {
        return this.leavesService.queryById(id);
    }


    @GetMapping("/toStuLeaves")
    public String getLeavesList(ModelMap modelMap){

        List<Leaves> stuLeaves = leavesService.getStuLeaves();

        modelMap.put("stuLeaves", stuLeaves);
        return "leaves";
    }


    @GetMapping("/agreement")
    public String agreementStu(String lid){
        int leaveId = Integer.parseInt(lid);

        leavesService.agreement(leaveId);

        return "leaves";
    }

    @PostMapping("/teacherLeave")
    public String teacherLeave(Leaves leaves, HttpServletRequest httpRequest){

        System.out.println(leaves);

        User user = (User) httpRequest.getSession().getAttribute("user");

        Teacher teacher = teacherService.getTeacherByUserId(user.getUserId());
        leaves.setTeaId(teacher.getTeaId());
        leaves.setLeaveTab(3);
        if (leaves.getLeaveReason() == null){
            leaves.setLeaveReason("瞎点的");
        }

        leavesService.teacherLeave(leaves);

        return "redirect:/";
    }

}